/**
 * web2010_admin
 * cn.aresoft.web.action.system.user UserAction.java
 */
package com.xsoft.web.action;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xsoft.framework.po.Result;
import com.xsoft.framework.util.Page;
import com.xsoft.framework.util.PageControl;
import com.xsoft.framework.web.BasicAction;
import com.xsoft.logic.dao.CommonDao;
import com.xsoft.logic.model.GameBatchData;
import com.xsoft.logic.model.GameRefBatch;
import com.xsoft.logic.service.interfaces.BatchGameService;
import com.xsoft.web.Exception.FileProcessException;
import com.xsoft.web.util.FileProcessUtil;


public class BatchGameAction extends BasicAction {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private CommonDao commonDao ;
	private GameRefBatch vo = new GameRefBatch();
	private List<GameRefBatch> dataList ;
	
	private GameBatchData vo2 = new GameBatchData();
	private List<GameBatchData> gameCodeList ;
	
	
	public String select(){
		
		StringBuffer hql = new  StringBuffer(" from GameRefBatch where  1=1 ");
		
		if(vo.getGameName()!=null && !"".equals(vo.getGameName())){
			hql.append(" and gameName='"+vo.getGameName()+"'");
		}
		if(vo.getFuliType()!=null && !"".equals(vo.getFuliType())){
			hql.append(" and fuliType='"+vo.getFuliType()+"'");
		}
		
		Page page = commonDao.pagedQuery(hql.toString(),gotoPage, pageSize,new Object[]{});
		dataList = (List<GameRefBatch>)page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	
	private String ___batchCode;
	private String ___codeStr;

	public String selectGameCodeList(){
		
		Map map = new HashMap();

		if(this.request.getParameter("batchCode")!=null){
			map.put("batchCode", this.request.getParameter("batchCode"));
		}
		
		Page page = commonDao.pagedQueryByMap(GameBatchData.class,gotoPage, pageSize,map);
		___batchCode = this.request.getParameter("batchCode");
		gameCodeList = (List<GameBatchData>)page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	
	private String codeStr,gameName,fuliType,starttime1,endtime1;
	
	public String selectGameCodeLogList(){
		
		StringBuffer hql = new  StringBuffer(" from GameBatchData where state=2 and 1=1 ");

		if(this.request.getParameter("codeStr")!=null && !"".equals(this.request.getParameter("codeStr"))){
			hql.append(" and codeStr='"+this.request.getParameter("codeStr")+"'");
		}
		
		if(this.request.getParameter("phoneNum")!=null && !"".equals(this.request.getParameter("phoneNum"))){
			hql.append(" and phoneNum='"+this.request.getParameter("phoneNum")+"'");
		}
		
		if(this.request.getParameter("gameName")!=null && !"".equals(this.request.getParameter("gameName"))){
			hql.append(" and gameName='"+this.request.getParameter("gameName")+"'");
		}
		
		if(this.request.getParameter("fuliType")!=null && !"".equals(this.request.getParameter("fuliType"))){
			hql.append(" and fuliType='"+this.request.getParameter("fuliType")+"'");
		}
		
		if(request.getParameter("starttime1")!=null && !"".equals(this.request.getParameter("starttime1"))){
			starttime1 = this.request.getParameter("starttime1");
			hql.append(" and getTime>='"+ this.request.getParameter("starttime1")+"'");
		}
		
		if(request.getParameter("endtime1")!=null && !"".equals(this.request.getParameter("endtime1"))){
			endtime1 = this.request.getParameter("endtime1") ;
			hql.append(" and getTime<='"+this.request.getParameter("endtime1")+"'");
		}
		
		System.out.println(hql.toString()+">>>>>");
		Page page = commonDao.pagedQuery(hql.toString(),gotoPage, pageSize,new Object[]{});
		gameCodeList = (List<GameBatchData>)page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	
	private File excel;
	private String excelFileName;
	private String excelContentType;
	
	private BatchGameService batchGameService ;
	
	
	/*
	 * 导入游戏礼包数据
	 */
	public String importGameData(){
		try {
			this.batchGameService.save(excel, excelFileName,this.request.getParameter("gameName"),this.request.getParameter("fuliType"));
		}catch (Exception e) {
			e.printStackTrace();
			this.writeJson(new Result(-1,"上传失败，只支持excel97到2003版本的excel "));
			return null;
		}
		this.writeJson(new Result(0,"ok"));
		return null;
	}
	
	public String selectVoById(){
		GameRefBatch am = (GameRefBatch)commonDao.get(GameRefBatch.class,Integer.parseInt(request.getParameter("id")));
		if(am.getStarttime()!=null){
			am.setStarttime1(sdf.format(am.getStarttime()));
			am.setEndtime1(sdf.format(am.getEndtime()));
		}
		writeJson(new Result(0,"ok",am));
		return null;
	}
	
	
	
	private File gameImg;
	private String gameImgFileName;
	private String gameImgContentType;
	
	public String setGameInfo(){
		
		if(! "1".equals(request.getParameter("hasImg"))){
			FileProcessUtil fu = new FileProcessUtil();
			String path = request.getRealPath("/") + "upload/";
			gameImgFileName = System.currentTimeMillis()+gameImgFileName.substring(gameImgFileName.lastIndexOf("."));
			try {
				fu.saveFile(gameImg,gameImgFileName, path, "");
			} catch (FileProcessException e) {
				e.printStackTrace();
				writeJson(new Result(-1,"上传图片失败"));
				return null;
			}
		}
		
		GameRefBatch am = (GameRefBatch)commonDao.get(GameRefBatch.class,Integer.parseInt(request.getParameter("id")));
		am.setTitle(this.request.getParameter("title"));
		am.setDescStr(this.request.getParameter("descStr"));
		
		try
		{
			am.setStarttime(sdf.parse(this.request.getParameter("starttime1")));
			am.setEndtime(sdf.parse(this.request.getParameter("endtime1")));
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		
		if(!"1".equals(request.getParameter("hasImg"))){
			am.setImgSrc(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/upload/"+gameImgFileName);
		}

		commonDao.save(am);
		writeJson(new Result(0,"ok"));
		return null;
	}
	
	//上/下架
	public String toUpdateState(){
		GameRefBatch am = (GameRefBatch)commonDao.get(GameRefBatch.class,Integer.parseInt(request.getParameter("id")));
		am.setState(request.getParameter("st"));
		commonDao.save(am);
		writeJson(new Result(0,"ok"));
		return null;
	}
	
	
	public String delete(){
		commonDao.removeById(GameRefBatch.class, Integer.parseInt(request.getParameter("id")));
		List<GameBatchData> list = (List<GameBatchData>)commonDao.find(" from GameBatchData where batchCode=?", new Object[]{request.getParameter("batchCode")});
		for(int i=0;i<list.size();i++){
			this.commonDao.remove(list.get(i));
		}
		writeJson(new Result(0,"ok"));
		return null;
	}
	

	//////////////////////////////////////////////////////////////////////////
	
	private String phoneNum;

	public String selectForApp(){
		
		phoneNum = this.request.getParameter("phoneNum");
		
		
		String game_flag = request.getParameter("game_flag");
		String fuli_type = request.getParameter("fuli_type");
		if(game_flag==null ||"".equals(game_flag)){
			game_flag="征途";
		}
		if(fuli_type==null ||"".equals(fuli_type)){
			fuli_type="1";
		}
		
		//查询最近三个月的数据-不做分页处理
		dataList =  (List<GameRefBatch>)commonDao.find("from GameRefBatch where gameName=? and fuliType=? and state=2 ",new Object[]{game_flag,fuli_type});
		
		return SUCCESS;
	}
	
	private String game_flag;
	private String fuli_type;
	
	GameRefBatch gameRefBatch;
	public String selectVoByIdForApp(){
		
		game_flag = request.getParameter("game_flag");
		fuli_type = request.getParameter("fuli_type");
				
		gameRefBatch = (GameRefBatch)commonDao.get(GameRefBatch.class,Integer.parseInt(request.getParameter("id")));
		
		String phoneNum = this.request.getParameter("phoneNum");
		PageControl page = new PageControl();
		page.setPagesize(1);
		List<GameBatchData> list = (List<GameBatchData>) commonDao.find("from GameBatchData where batchCode=? and phoneNum=? and state=2  ", page ,new Object[]{gameRefBatch.getBatchCode(),phoneNum});
		if(list.size()>0){
			gameRefBatch.setFlag("1");
		}
		
		return SUCCESS;
	}
	
	
	
	
	public String toTryGetFuli(){
		String batchCode = this.request.getParameter("batchCode");
		String phoneNum = this.request.getParameter("phoneNum");
		PageControl page = new PageControl();
		page.setPagesize(1);
		List<GameBatchData> list = (List<GameBatchData>) commonDao.find("from GameBatchData where batchCode=? and state=1 order by id  ", page ,new Object[]{batchCode});
		if(list.size()>0){
			GameBatchData gd = list.get(0);
			gd.setPhoneNum(phoneNum);
			gd.setGetTime(new Date());
			gd.setState("2");
			commonDao.save(gd);
			
			List<GameRefBatch> list2 = (List<GameRefBatch>) commonDao.find("from GameRefBatch where batchCode=? and state=2 " ,new Object[]{batchCode});
			GameRefBatch grb = list2.get(0);
			grb.setUseCount(grb.getUseCount()+1);
			commonDao.save(grb);
			
			writeJson(new Result(0,"ok"));
		}
		return null;
	}
	
	
	private List<GameBatchData> myGameList;
	
	public String selectMyGameApp(){
		
		game_flag = request.getParameter("game_flag");
		fuli_type = request.getParameter("fuli_type");
		
		phoneNum = this.request.getParameter("phoneNum");
		
		myGameList =  (List<GameBatchData>)commonDao.find("from GameBatchData where phoneNum=?  order by getTime desc ",new Object[]{phoneNum});
		
		return SUCCESS;
	}
	
	

	
	//////////////////////////////////////////////////////////////////////////
	

	public String get___codeStr() {
		return ___codeStr;
	}


	public List<GameBatchData> getMyGameList()
	{
		return myGameList;
	}


	public void setMyGameList(List<GameBatchData> myGameList)
	{
		this.myGameList = myGameList;
	}


	public void set___codeStr(String str) {
		___codeStr = str;
	}


	public File getGameImg() {
		return gameImg;
	}


	public void setGameImg(File gameImg) {
		this.gameImg = gameImg;
	}


	public String getGame_flag()
	{
		return game_flag;
	}


	public void setGame_flag(String game_flag)
	{
		this.game_flag = game_flag;
	}


	public String getFuli_type()
	{
		return fuli_type;
	}


	public void setFuli_type(String fuli_type)
	{
		this.fuli_type = fuli_type;
	}


	public String getGameImgFileName() {
		return gameImgFileName;
	}


	public void setGameImgFileName(String gameImgFileName) {
		this.gameImgFileName = gameImgFileName;
	}


	public String getGameImgContentType() {
		return gameImgContentType;
	}


	public void setGameImgContentType(String gameImgContentType) {
		this.gameImgContentType = gameImgContentType;
	}


	public File getExcel()
	{
		return excel;
	}



	public void setExcel(File excel)
	{
		this.excel = excel;
	}



	public String getExcelFileName()
	{
		return excelFileName;
	}



	public void setExcelFileName(String excelFileName)
	{
		this.excelFileName = excelFileName;
	}



	public String getExcelContentType()
	{
		return excelContentType;
	}



	public void setExcelContentType(String excelContentType)
	{
		this.excelContentType = excelContentType;
	}



	public BatchGameService getBatchGameService()
	{
		return batchGameService;
	}
	
	public String get___batchCode() {
		return ___batchCode;
	}


	public void set___batchCode(String code) {
		___batchCode = code;
	}



	public void setBatchGameService(BatchGameService batchGameService)
	{
		this.batchGameService = batchGameService;
	}



	public GameBatchData getVo2()
	{
		return vo2;
	}



	public void setVo2(GameBatchData vo2)
	{
		this.vo2 = vo2;
	}

	public CommonDao getCommonDao()
	{
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao)
	{
		this.commonDao = commonDao;
	}


	public GameRefBatch getVo()
	{
		return vo;
	}


	public void setVo(GameRefBatch vo)
	{
		this.vo = vo;
	}



	public List<GameRefBatch> getDataList()
	{
		return dataList;
	}



	public void setDataList(List<GameRefBatch> dataList)
	{
		this.dataList = dataList;
	}



	public List<GameBatchData> getGameCodeList()
	{
		return gameCodeList;
	}



	public void setGameCodeList(List<GameBatchData> gameCodeList)
	{
		this.gameCodeList = gameCodeList;
	}

	public GameRefBatch getGameRefBatch()
	{
		return gameRefBatch;
	}

	public void setGameRefBatch(GameRefBatch gameRefBatch)
	{
		this.gameRefBatch = gameRefBatch;
	}
	
	public String getPhoneNum()
	{
		return phoneNum;
	}


	public String getCodeStr()
	{
		return codeStr;
	}

	public void setCodeStr(String codeStr)
	{
		this.codeStr = codeStr;
	}

	public String getGameName()
	{
		return gameName;
	}

	public void setGameName(String gameName)
	{
		this.gameName = gameName;
	}

	public String getFuliType()
	{
		return fuliType;
	}

	public void setFuliType(String fuliType)
	{
		this.fuliType = fuliType;
	}

	public String getStarttime1()
	{
		return starttime1;
	}

	public void setStarttime1(String starttime1)
	{
		this.starttime1 = starttime1;
	}

	public String getEndtime1()
	{
		return endtime1;
	}

	public void setEndtime1(String endtime1)
	{
		this.endtime1 = endtime1;
	}

	public void setPhoneNum(String phoneNum)
	{
		this.phoneNum = phoneNum;
	}
	
	
	public static void main(String[] args)
	{
		 	String s = new String("bc");
	        
	        
	        
	}
	
	
}
